package leetcode.剑指offer.前40;

import leetcode.剑指offer.pojo.ListNode;
import org.junit.Test;

/**
 * @author :songyaozhang
 * @date :Created 2021/7/28 19:03
 * @description :https://leetcode-cn.com/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof/
 */
public class 剑指_25_合并两个排序的链表 {
    @Test
    public void test() {
        Solution solution = new Solution();
        ListNode root = solution.mergeTwoLists(new ListNode(1, new ListNode(2, new ListNode(4))), new ListNode(1, new ListNode(3, new ListNode(4))));
        while (root != null) {
            System.out.println(root.val);
            root = root.next;
        }
    }


    class Solution {
        public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            ListNode head = new ListNode(-1);
            ListNode tmp = head;
            while (l1 != null && l2 != null) {
                tmp.next = l1.val < l2.val ? l1 : l2;
                if (l1.val < l2.val) {
                    l1 = l1.next;
                } else {
                    l2 = l2.next;
                }
                tmp = tmp.next;
            }
            tmp.next = l1 == null ? l2 : l1;
            return head.next;
        }
    }

}
